package com.ruoyi.project.wx.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.bootdo.UuidUtil;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.project.wx.dao.MemberTokenDao;
import com.ruoyi.project.wx.dao.SetWechatDao;
import com.ruoyi.project.wx.domain.MemberTokenDO;
import com.ruoyi.project.wx.domain.SetWechatDO;
import com.ruoyi.project.wx.service.MemberTokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class MemberTokenServiceImpl implements MemberTokenService {
	@Autowired
	private MemberTokenDao memberTokenDao;
	@Autowired
	private SetWechatDao setWechatDao;
	
	@Override
	public MemberTokenDO get(String id){
		return memberTokenDao.get(id);
	}
	
	@Override
	public List<MemberTokenDO> list(Map<String, Object> map){
		return memberTokenDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return memberTokenDao.count(map);
	}
	
	@Override
	public int save(MemberTokenDO memberToken){
		return memberTokenDao.save(memberToken);
	}
	
	@Override
	public int update(MemberTokenDO memberToken){
		return memberTokenDao.update(memberToken);
	}
	
	@Override
	public int remove(String id){
		return memberTokenDao.remove(id);
	}
	
	@Override
	public int batchRemove(String[] ids){
		return memberTokenDao.batchRemove(ids);
	}

	@Override
	public String getToken() {
		MemberTokenDO tokenDO = memberTokenDao.getToken();
		if (tokenDO != null) {
			Date date = tokenDO.getCreateAt();
			int expiresIn = tokenDO.getExpiresIn();
			int oldTime = (int)(date.getTime()/1000)+expiresIn-300;
			int newTime = (int)(System.currentTimeMillis()/1000);
			if (newTime < oldTime) {
				return tokenDO.getToken();
			}
		}
		//重新获取token
		String url = "https://api.weixin.qq.com/cgi-bin/token";
		SetWechatDO setWechatDO = setWechatDao.list(new HashMap<>()).get(0);
		String appId = setWechatDO.getAppId();
		String appSecret = setWechatDO.getAppSecret();
		String getUrl = url+"?grant_type=client_credential&appid="+appId+"&secret="+appSecret;
		try {
			String s = HttpUtils.sendGet(getUrl , "");
			/*String body = response.body();
			Gson gson = new Gson();
			Map<String, Object> map = new HashMap<String, Object>();
			map = gson.fromJson(body, map.getClass());*/
			System.out.print("获取token结果：："+s.toString());
			JSONObject jSONObject = (JSONObject)JSONObject.parse(s);
			String access_token = jSONObject.get("access_token").toString();
			int expires_in = Double.valueOf(jSONObject.get("expires_in").toString()).intValue();
			MemberTokenDO memberTokenDO = new MemberTokenDO();
			memberTokenDO.setToken(access_token);
			memberTokenDO.setExpiresIn(expires_in);
			memberTokenDO.setCreateAt(new Date());
			if (tokenDO != null){
				memberTokenDO.setId(tokenDO.getId());
				memberTokenDao.update(memberTokenDO);
			} else {
				memberTokenDO.setId(UuidUtil.get32UUID());
				memberTokenDao.save(memberTokenDO);
			}
			return access_token;
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}
	
}
